package com.woniu.car.controller;

import com.woniu.car.annotation.BackStageAccountCheck;
import com.woniu.car.annotation.BackStageLoginCheck;
import com.woniu.car.common.BeanCopyUtils;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.domain.TBackstageAccount;
import com.woniu.car.dto.BackstageAccountDto;
import com.woniu.car.param.LoginBackstageAccountParam;
import com.woniu.car.param.RegisterBackstageAccountParam;
import com.woniu.car.service.TBackstageAccountService;
import com.woniu.car.util.Code;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WWX
 * @since 2021-06-19
 */
@Api(tags = "后台账号控制器")
@RestController
@RequestMapping("/tBackstageAccount")
public class TBackstageAccountController {
    @Resource
    private TBackstageAccountService bas;

    /**
     *@Author 王维鑫
     *@Description 注册账号
     *@Param
     *@return
     **/
    @BackStageAccountCheck
    @ApiOperation("后台账号注册接口")
    @PostMapping("register")
    public ResponseEntity<BackstageAccountDto> registerBackstageAccount(RegisterBackstageAccountParam param) throws Exception {
        TBackstageAccount tBackstageAccount = bas.registerAccount(param);
        BackstageAccountDto backstageAccountDto = new BackstageAccountDto();
        BeanCopyUtils.copyProperties(tBackstageAccount,backstageAccountDto);
        return new ResponseEntity<BackstageAccountDto>().setObject(backstageAccountDto).setCode(Code.NORMAL).setMsg("注册成功");
    }
    /**
     *@Author 王维鑫
     *@Description 账号登录
     *@Param
     *@return
     **/
    @BackStageLoginCheck
    @ApiOperation("后台账号登录接口")
    @GetMapping("login")
    public ResponseEntity<BackstageAccountDto> loginBackstageAccount(LoginBackstageAccountParam param) throws Exception {
        TBackstageAccount login = bas.login(param);
        BackstageAccountDto backstageAccountDto = new BackstageAccountDto();
        BeanCopyUtils.copyProperties(login,backstageAccountDto);
        Integer backstageId = login.getBackstageId();
        String token = JWTUtil.generatorBackJWT(backstageId.toString());
        backstageAccountDto.setToken(token);
        return ResponseEntity.builder(BackstageAccountDto.class).setObject(backstageAccountDto).setCode(Code.NORMAL).setMsg("登录成功");
    }
    @BackStageAccountCheck
    @ApiOperation("超级管理员账号注册")
    @GetMapping("registerSuperAccount")
    public ResponseEntity registerSuperAccount(RegisterBackstageAccountParam param){
        TBackstageAccount tBackstageAccount1 = new TBackstageAccount();
        BeanCopyUtils.copyProperties(param,tBackstageAccount1);
        TBackstageAccount tBackstageAccount = bas.insertSuperAccount(tBackstageAccount1);
        return ResponseEntity.builder(TBackstageAccount.class).setObject(tBackstageAccount).setCode(Code.NORMAL).setMsg("超级管理员注册成功");
    }





}

